import mqtt from 'mqtt';
import { VITE_APP_MQTT_BASE_URL } from './base'
import { useUserStoreHook } from '@/store/modules/user';
const userStore = useUserStoreHook();
export type MqttClient = mqtt.MqttClient;
export type OnMessageCallback = mqtt.OnMessageCallback;

const mqttHttp = `${window.IPConfig.MQTTIP}`


export const createMqttClient = (params: any = {}): MqttClient => {
  const options = {
    connectTimeout: 5000,
    clientId: 'c' + userStore.userInfo.id+Math.floor(Math.random() * 100000) ,//唯一ID 
    // username, //账号 非必填 看你们设置没有
    // password, //密码 非必填
    clean: true,
  }
  const client = mqtt.connect(mqttHttp, options);

  // 添加连接、错误、消息接收等事件监听器
  client.on('connect', () => console.log('连接mqtt成功==>', params.url));
  client.on('error', (error) => console.error('打印mqtt错误:', error));

  return client;
};